package com.example.demo.controller;

import com.example.demo.log.Log;
import com.example.demo.log.LogModuleEnum;
import com.example.demo.model.core.Add;
import com.example.demo.model.core.PageVo;
import com.example.demo.model.core.R;
import com.example.demo.model.core.Update;
import com.example.demo.model.form.ConfigForm;
import com.example.demo.model.query.ConfigQuery;
import com.example.demo.model.vo.ConfigVO;
import com.example.demo.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/config")
@Tag(name = "系统配置接口")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPerm('sys:config:add')")
    @Operation(summary = "新增系统配置")
    @Log(value = "新增系统配置", module = LogModuleEnum.SETTING)
    public R<Void> add(@Validated(Add.class) @RequestBody ConfigForm form) {
        sysConfigService.add(form);
        return R.ok();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('sys:config:edit')")
    @Operation(summary = "修改系统配置")
    @Log(value = "修改系统配置", module = LogModuleEnum.SETTING)
    public R<Void> update(@Validated(Update.class) @RequestBody ConfigForm form) {
        sysConfigService.update(form);
        return R.ok();
    }

    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPerm('sys:config:delete')")
    @Operation(summary = "删除系统配置")
    @Log(value = "删除系统配置", module = LogModuleEnum.SETTING)
    public R<Void> delete(@Schema(description = "系统配置ID数组") @RequestBody List<String> ids) {
        sysConfigService.delete(ids);
        return R.ok();
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('sys:config:query')")
    @Operation(summary = "系统配置列表")
    public R<PageVo<ConfigVO>> page(@Validated ConfigQuery query) {
        PageVo<ConfigVO> page = sysConfigService.page(query);
        return R.ok(page);
    }

    @GetMapping("/form")
    @Operation(summary = "系统配置表单")
    public R<ConfigForm> form(@Parameter(description = "系统配置ID") String id) {
        ConfigForm form = sysConfigService.form(id);
        return R.ok(form);
    }
}
